﻿@{
    ViewBag.Title = "ItemForm";
    Layout = "~/Views/Shared/_Form.cshtml";
}
<!--jqgrid表格组件start-->
<link href="~/Content/scripts/plugins/jqgrid/jqgrid.css" rel="stylesheet" />
<script src="~/Content/scripts/plugins/jqgrid/grid.locale-cn.js"></script>
<script src="~/Content/scripts/plugins/jqgrid/jqgrid.min.js"></script>
<!--表格组件end-->
<script>
    var keyValue = request('keyValue');
    var _BillType = request('BillType');
    var _ResponDepts = "", _Type = "", _OrganizeId = "@LeSoft.Application.Code.OperatorProvider.Provider.Current().CompanyId", _CategoryId = "", NeedSortFlag = "";
    var _rowid = 1;

    $(function () {
        
        initControl();
        GetGrid();
        GetGrid1();
    })

    //初始化控件
    function initControl() {
        //组织机构
        $("#OrganizeId").ComboBoxTree({
            url: "../../BaseManage/Organize/GetTreeJsonById",
            description: "==请选择==",
            height: "200px",
            click: function (item) {
                _OrganizeId = item.id;
                GetTree();
            }
        });
        if (_OrganizeId != "") $("#OrganizeId").ComboBoxSetValue(_OrganizeId);

        var _url = "";
        var _CategoryUrl = "";
        if (_BillType == "1") {//开店
            _url = "/DailyEventManage/OpenShop/GetDataItemTreeJson?EnCode=OpenShopWorkType";
            _CategoryUrl = "../../SystemManage/DataItemDetail/GetDataItemTreeJson?EnCode=OpenShopType";
        }
        else if (_BillType == "2") {//闭店
            _url = "/DailyEventManage/OpenShop/GetDataItemTreeJson?EnCode=CloseShopWorkType";
            _CategoryUrl = "../../SystemManage/DataItemDetail/GetDataItemTreeJson?EnCode=CloseShopType";
        }
        else {
            _url = "";
            _CategoryUrl = "";
        }
        //类别
        $("#Type").ComboBoxTree({
            url: _url,
            description: "==请选择==",
            height: "200px",
            click: function (item) {
                _Type = item.id;
                _NeedSortFlag = item.NeedSortFlag;
                if (_NeedSortFlag == "1") {
                    $("#SortCode").attr("isvalid", "yes").attr("checkexpession", "NotNull");
                }
                else { $("#SortCode").removeAttr("isvalid").removeAttr("checkexpession"); }
            }
        });

        GetTree();

        //工作阶段
        $("#CategoryId").ComboBoxTree({
            url: _CategoryUrl,
            description: "==请选择==",
            height: "110px",
            click: function (item) {
                _CategoryId = item.id;
            }
        });

        //获取表单
        if (!!keyValue) {
            $.SetForm({
                url: "../../DailyEventManage/OpenShop/GetItemEntity",
                param: { keyValue: keyValue },
                success: function (data) {
                    
                    $("#form1").SetWebControls(data);
                    $("#OrganizeId").ComboBoxSetValue(data.OrganizeId);
                    $("#ResponDepts").ComboBoxSetValue(data.ResponDepts);
                    $("#CategoryId").ComboBoxSetValue(data.CategoryId);
                    $("#Type").ComboBoxSetValue(data.TypeId);
                    _Type = data.TypeId;
                    _OrganizeId = data.OrganizeId;
                    _ResponDepts = data.ResponDepts;
                    _CategoryId = data.CategoryId;
                    $("#BeginTime").val(data.BeginTime);
                    $("#EndTime").val(data.EndTime);
                }
            });
        }
    }

    function GetTree() {
        //责任部门
        $("#ResponDepts").ComboBoxTree({
            url: "../../BaseManage/Department/GetTreeJson?organizeId=" + _OrganizeId,
            description: "==请选择==",
            height: "110px",
            click: function (item) {
                _ResponDepts = item.id;
            }
        });
    }

    function GetGrid() {
        
        var $gridTable = $("#gridTable");
        $gridTable.jqGrid({
            datatype: "json",
            url: " ../../DailyEventManage/OpenShop/GetItemObjectList?ItemId=" + keyValue,
            unwritten: false,
            height:400,
            colModel: [
               { label: "Id", name: "Id", hidden: true },
               { label: "ObjectTypeId", name: "ObjectTypeId", hidden: true },
               {
                   label: " ", name: "delete", index: "delete", width: 30, align: "center",
                   formatter: function (cellvalue, options, rowObject) {
                       return "<a onclick=\"btn_delete_Obj(" + options.rowId + ");\"><i class=\"fa fa-trash\"></i>&nbsp;</a>"
                   }
               },
               { label: "类别", name: "ObjectTypeName", width: 120, idnex: "ObjectTypeName", align: "center", sortable: false },
               { label: "名称", name: "Name", width: 120, idnex: "Name", align: "center", sortable: false },
               { label: "编号", name: "Code", width: 120, idnex: "Code", align: "center", sortable: false },
               { label: "具体描述", name: "OtherCode", width: 420, idnex: "OtherCode", align: "center", sortable: false },
               {
                   label: "是否需要附件", name: "PictureFlag", width: 120, idnex: "PictureFlag", align: "center", sortable: false,
                   formatter: function (cellvalue, options, rowObject) {
                       return cellvalue == 1 ? "<i class=\"fa fa-toggle-on\"></i>" : "<i class=\"fa fa-toggle-off\"></i>";
                   },
               },
               { label: "ObjectId", name: "ObjectId", hidden: true },
               { label: "ItemId", name: "ItemId", hidden: true },
               { label: "ContentId", name: "ContentId", hidden: true }
            ],
            rowNum: 1000,
            rownumbers: true,
            shrinkToFit: false,
            gridview: true,
            hidegrid: false,
            onCellSelect: function (rowid, iCol, cellcontent, e) {
                if (iCol == 9) {
                    if (cellcontent == "<i class=\"fa fa-toggle-on\"></i>") $gridTable.setCell(rowid, iCol, "0");
                    else $gridTable.setCell(rowid, iCol, "1");
                }
            },
            subGrid: true,
            subGridRowExpanded: function (subgrid_id, row_id) {
                if ($("#gridTable").jqGrid('getRowData', row_id)['ObjectTypeId'] != "5") {
                    var ContentId = $("#gridTable").jqGrid('getRowData', row_id)['ContentId'];
                    var subgrid_table_id = subgrid_id + "_t";
                    var subhtml = "";
                    subhtml += '<div><table id="' + subgrid_table_id + '"></table></table></div>';
                    $("#" + subgrid_id).html(subhtml);
                    $("#" + subgrid_table_id).jqGrid({
                        caption: "内容",
                        datatype: "json",
                        height: 100,
                        colModel: [
                            { label: "主键", name: "Id", index: "Id", hidden: true },
                            {
                                label: " ", name: "delete", index: "delete", width: 30, align: "center",
                                formatter: function (cellvalue, options, rowObject) {
                                    return "<a onclick=\"RemoveGrid(" + options.rowId + ",'" + subgrid_table_id + "'," + row_id + ");\"><i class=\"fa fa-trash\"></i>&nbsp;</a>"
                                }
                            },
                            { label: "编号", name: "Code", index: "Code", width: 60, align: "center" },
                            { label: "名称", name: "Name", index: "Name", width: 70, align: "center" },
                            {
                                label: "类型", name: "Type", index: "Type", width: 70, align: "center",
                                formatter: function (cellvalue, options, rowObject) {
                                    return cellvalue == 0 ? "录入型" : "选择型";
                                }
                            },
                            {
                                label: "内容详情", name: "TypeDecription", index: "TypeDecription", width: 100, align: "left",
                                formatter: function (cellvalue, options, rowObject) {
                                    var obj = JSON.parse(cellvalue);
                                    var _Html = "";
                                    if (rowObject.Type == 1) {
                                        $.each(obj, function (index, item) {
                                            _Html += "<div style=\"float: left; margin-left: 20px;\">" + item.Text + "</div>";
                                        });
                                        return _Html;
                                    }
                                    else {
                                        if (obj[0].ValueType == "文本型") {
                                            _Html = "默认值：";
                                            _Html += obj[0].DefaultValue == "" ? "无" : obj[0].DefaultValue;
                                        }
                                        else {
                                            _Html = "最小值：";
                                            _Html += obj[0].MinValue == "" ? "无" : obj[0].MinValue;
                                            _Html += ",最大值：";
                                            _Html += obj[0].MaxValue == "" ? "无" : obj[0].MaxValue;
                                        }
                                    }
                                    return _Html;
                                }
                            },
                        ],
                        pager: false,
                        rowNum: "1000",
                        rownumbers: true,
                        hidegrid: false,
                        shrinkToFit: false,
                        gridview: true
                    });
                    //获取表单
                    if (!!ContentId) {
                        $.SetForm({
                            url: "/DailyEventManage/OpenShop/GetContentList",
                            param: { ids: ContentId },
                            success: function (data) {
                                $("#" + subgrid_table_id)[0].addJSONData(data);
                            }
                        });
                    }
                }
            }
        });
    }

    function GetGrid1() {
        var $gridTable = $("#gridTable1");
        $gridTable.jqGrid({
            datatype: "json",
            url: " ../../DailyEventManage/OpenShop/GetItemRelationList?ItemId=" + keyValue + "&Category=" + _BillType,
            unwritten: false,
            height: 250,
            colModel: [
                { label: "主键", name: "Id", index: "Id", hidden: true },
                { label: "主键", name: "MItemId", index: "MItemId", hidden: true },
                { label: "主键", name: "TypeId", index: "TypeId", hidden: true },
                {
                    label: " ", name: "delete", index: "delete", width: 30, align: "center",
                    formatter: function (cellvalue, options, rowObject) {
                        return "<a onclick=\"btn_delete_Regulatory(" + options.rowId + ");\"><i class=\"fa fa-trash\"></i>&nbsp;</a>"
                    }
                },
                { label: "编号", name: "Code", index: "Code", width: 70, align: "center" },
                { label: "名称", name: "Name", index: "Name", width: 100, align: "center" },
                { label: "工作阶段", name: "CategoryId", index: "CategoryId", width: 100, align: "center" },
                { label: "责任部门", name: "ResponDepts", index: "ResponDepts", width: 70, align: "center" }
            ],
            rowNum: 1000,
            rownumbers: true,
            shrinkToFit: false,
            gridview: true,
            hidegrid: false
        });
    }

    //保存表单
    function AcceptClick() {
        if (!$('#form1').Validform()) {
            return false;
        }
        var postData = $("#form1").GetWebControls(keyValue);

        
        postData["Category"] = _BillType;
        postData["OrganizeId"] = _OrganizeId;
        postData["ResponDepts"] = _ResponDepts;
        postData["CategoryId"] = _CategoryId;
        postData["TypeId"] = _Type;
        var obj = $("#gridTable").jqGrid("getRowData");
        var mItem = $("#gridTable1").jqGrid("getRowData");
        var ItemObject = [];
        var sort = 1;
        for (var i = 0; i < obj.length; i++) {
            var Contentids = obj[i].ContentId.split(',');
            var ids = obj[i].Id.split(',');
            if (!!Contentids && Contentids != "") {
                $.each(Contentids, function (index, model) {
                    if (model != "") {
                        var item = {};
                        item["ObjectId"] = obj[i].ObjectId;
                        item["ObjectTypeId"] = obj[i].ObjectTypeId;
                        item["PictureFlag"] = obj[i].PictureFlag == "<i class=\"fa fa-toggle-on\"></i>" ? 1 : 0;
                        item["SortCode"] = sort;
                        item["ContentId"] = model;
                        item["ObjectName"] = obj[i].Name;
                        if (ids != null && ids != "" && ids.length > index)
                            item["Id"] = ids[index];
                        if (!!keyValue)
                            item["ItemId"] = keyValue;
                        ItemObject.push(item);
                    }
                });
                sort++;
            }
            else {
                var item = {};
                item["ObjectId"] = obj[i].ObjectId;
                item["ObjectTypeId"] = obj[i].ObjectTypeId;
                item["PictureFlag"] = obj[i].PictureFlag == "<i class=\"fa fa-toggle-on\"></i>" ? 1 : 0;
                item["SortCode"] = sort;
                item["Id"] = ids[0];
                item["ObjectName"] = obj[i].Name;
                if (!!keyValue)
                    item["ItemId"] = keyValue;
                ItemObject.push(item);
                sort++;
            }
        }
        if (!!keyValue) {
            for (var i = 0; i < mItem.length; i++) {
                mItem[i].ItemId = keyValue;
                mItem[i].delete = "";
                mItem[i].PictureFlag = mItem[i].PictureFlag == "<i class=\"fa fa-toggle-on\"></i>" ? 1 : 0;
            }
        }
        
        $.SaveForm({
            url: "../../DailyEventManage/OpenShop/ItemSaveForm",
            param: { keyValue:keyValue,shopItemStr: JSON.stringify(postData), obj: JSON.stringify(ItemObject), mItemStr: JSON.stringify(mItem) },
            loading: "正在保存数据...",
            success: function () {
                $.currentIframe().$("#gridTable").trigger("reloadGrid");
            }
        });
    }

    function TopMost() {
        //var $tr = $(obj).parent().parent();
        var id = $('#gridTable').jqGrid('getGridParam', 'selrow');
        if (checkedRow(id)) {
            $("#gridTable tr td.sgexpanded").trigger("click");
            var $tr = $('#gridTable tr#' + id);
            if ($tr.index() != 1)
                $tr.prevAll("tr:last").after($tr);
            $tr.getGridParam('selrow');
        }
    }

    function SortUp() {
        //var $tr = $(obj).parent().parent();
        var id = $('#gridTable').jqGrid('getGridParam', 'selrow');
        if (checkedRow(id)) {
            $("#gridTable tr td.sgexpanded").trigger("click");
            var $tr = $('#gridTable tr#' + id);
            if ($tr.index() != 1)
                $tr.prev().before($tr);
            $tr.getGridParam('selrow');
        }
    }


    function SortDown() {
        //var $tr = $(obj).parent().parent();
        var id = $('#gridTable').jqGrid('getGridParam', 'selrow');
        if (checkedRow(id)) {
            $("#gridTable tr td.sgexpanded").trigger("click");
            var $tr = $('#gridTable tr#' + id);
            $tr.next().after($tr);
            $tr.getGridParam('selrow');
        }
    }

    function DownMost() {
        //var $tr = $(obj).parent().parent();
        var id = $('#gridTable').jqGrid('getGridParam', 'selrow');
        if (checkedRow(id)) {
            $("#gridTable tr td.sgexpanded").trigger("click");
            var $tr = $('#gridTable tr#' + id);
            $tr.nextAll("tr:last").after($tr);
            $tr.getGridParam('selrow');
        }
    }

    //添加
    function btn_add_Area() {
        if ($("#OrganizeId").attr('data-value')) {
            dialogOpen({
                id: "ContentItemForm",
                title: '添加',
                url: '/DailyEventManage/OpenShop/ShopItemForm?organizeId=' + _OrganizeId + "&rowid=" + _rowid,
                width: "1100px",
                height: "550px",
                callBack: function (iframeId) {
                    top.frames[iframeId].AcceptClick();
                }
            });
        }
        else {
            dialogMsg('请选择所属机构！', 0);
        }
    };

    function btn_add_content() {
        var id = $('#gridTable').jqGrid('getGridParam', 'selrow');
        var _url = "";
        if ($("#gridTable").jqGridRowValue("ObjectTypeId") != "5" && !!id) {
            _url = '/DailyEventManage/OpenShop/AddContent?organizeId=' + _OrganizeId + "&Category=" + _BillType + "&rowid=" + _rowid + "&row_id=" + id;
        }
        else {
            _url = '/DailyEventManage/OpenShop/AddContent?organizeId=' + _OrganizeId + "&Category=" + _BillType + "&rowid=" + _rowid;
        }
        if ($("#OrganizeId").attr('data-value')) {
            dialogOpen({
                id: "ContentIndexForm",
                title: '添加',
                url: _url,
                width: "1100px",
                height: "550px",
                callBack: function (iframeId) {
                    top.frames[iframeId].AcceptClick();
                }
            });
        }
        else {
            dialogMsg('请选择所属机构！', 0);
        }
    }

    //移除
    function btn_delete_Obj(rowid) {
        if (rowid) {
            dialogConfirm('注：您确定要移除吗？该操作将无法恢复？', function (r) {
                if (r) {
                    $("#gridTable").delRowData(rowid);
                    dialogMsg("移除成功。", 1);
                }
            });
        }
        else {
            dialogMsg('请选择需要移除的数据！', 0);
        }
    }

    function btn_add_Regulatory() {
        if ($("#OrganizeId").attr('data-value') && !!_Type) {
            dialogOpen({
                id: "RegulatoryForm",
                title: '添加',
                url: '/DailyEventManage/OpenShop/RegulatoryForm?organizeId=' + _OrganizeId + "&TypeId=" + _Type + "&Category=" + _BillType + "&ItemId=" + keyValue,
                width: "1100px",
                height: "550px",
                callBack: function (iframeId) {
                    top.frames[iframeId].AcceptClick();
                }
            });
        }
        else {
            dialogMsg('请选择所属机构和工作类型！', 0);
        }
    }

    //移除
    function btn_delete_Regulatory(rowid) {
        if (rowid) {
            dialogConfirm('注：您确定要移除吗？该操作将无法恢复？', function (r) {
                if (r) {
                    $("#gridTable1").delRowData(rowid);
                    dialogMsg("移除成功。", 1);
                }
            });
        } else {
            dialogMsg('请选择需要移除的数据！', 0);
        }
    }

    function RemoveGrid(rowid, tableId, row_id) {
        
        var id = $('#' + tableId).getRowData(rowid).Id;
        var obj = $('#gridTable').getRowData(row_id);
        var ids = obj.ContentId.split(',');
        if (!!ids && ids != "") {
            $.each(ids, function (index, model) {
                if (model != "" && model == id) {
                    ids.splice(index, 1);
                    return false;
                }
            });
        }
        ids = ids.join(',') == "" ? "&nbsp;" : ids.join(',');
        $("#gridTable1").setCell(row_id, 'ContentId', ids);
        $('#' + tableId).delRowData(rowid);
    }
</script>

<div style="margin-top: 10px;">
    @*style="border: 1px solid #BEBEBE; background-color: #f4f4f4;margin:10px;"*@
    <div>
        <table class="form">
            <tr>
                <th class="formTitle">工作类型<font face="宋体">*</font></th>
                <td class="formValue">
                    <div id="Type" type="select" class="ui-select" isvalid="yes" checkexpession="NotNull"></div>
                </td>
                <th class="formTitle">所属机构<font face="宋体">*</font></th>
                <td class="formValue">
                    <div id="OrganizeId" type="select" class="ui-select" isvalid="yes" checkexpession="NotNull"></div>
                </td>
            </tr>
            <tr>
                <th class="formTitle">项目名称<font face="宋体">*</font></th>
                <td class="formValue">
                    <input id="Name" type="text" class="form-control" isvalid="yes" checkexpession="NotNull" />
                </td>
                <th class="formTitle">项目编码<font face="宋体">*</font></th>
                <td class="formValue">
                    <input id="Code" type="text" class="form-control" isvalid="yes" checkexpession="NotNull" />
                </td>
            </tr>
            <tr>
                <th class="formTitle">开始时间<font face="宋体">*</font></th>
                <td class="formValue">
                    <input id="BeginTime" type="text" class="form-control input-datepicker" isvalid="yes" checkexpession="NotNull" onfocus="WdatePicker({ dateFmt: 'HH:mm:ss' })" />
                </td>
                <th class="formTitle">结束时间<font face="宋体">*</font></th>
                <td class="formValue">
                    <input id="EndTime" type="text" class="form-control input-datepicker" isvalid="yes" checkexpession="NotNull" onfocus="WdatePicker({ dateFmt: 'HH:mm:ss' })" />
                </td>
            </tr>
            <tr>
                <th class="formTitle">所属工作阶段</th>
                <td class="formValue">
                    <div id="CategoryId" type="select" class="ui-select"></div>
                </td>
                <th class="formTitle">责任部门</th>
                <td class="formValue">
                    <div id="ResponDepts" type="select" class="ui-select"></div>
                </td>
            </tr>
            <tr>
                <th class="formTitle">选项<font face="宋体">*</font></th>
                <td class="formValue">
                    <input id="PictureFlag" type="checkbox" isvalid="yes" checkexpession="NotNull" />是否需要附件
                    <input id="NeedSortFlag" type="checkbox" isvalid="yes" checkexpession="NotNull" />内容是否顺序执行
                </td>
                <th class="formTitle">排序</th>
                <td class="formValue">
                    <input id="SortCode" type="number" class="form-control" />
                </td>
            </tr>
            <tr>
                <th class="formTitle">工作标准</th>
                <td class="formValue" colspan="3">
                    <textarea id="Criterion" class="form-control" style="height: 50px;"></textarea>
                </td>
            </tr>
            <tr>
                <td colspan="4" style="padding-top:1px;">
                    <div class="panel-Title" style="border:0px;">
                        执行明细
                        <div class="btn-group" style="float:right;margin-right:10px;">
                            <a id="lr-addObj" class="btn btn-default" onclick="btn_add_Area()"><i class="fa fa-plus"></i>&nbsp;添加点位</a>
                            <a id="lr-addContentIndex" class="btn btn-default" onclick="btn_add_content()"><i class="fa fa-plus"></i>&nbsp;添加内容</a>
                            @*<script>$('.btn-group').authorizeButton()</script>*@
                        </div>
                    </div>
                    <div style="margin-left:10px;">
                        <div style="width:94%;float:left;">
                            <table id="gridTable"></table>
                        </div>
                        <div style="width: 6%; float: right;">
                            <a class="btn btn-default" onclick="TopMost();"><i class="fa fa-arrow-up"></i>最上</a><br />
                            <a class="btn btn-default" onclick="SortUp();"><i class="fa fa-arrow-up"></i>向上</a><br />
                            <a class="btn btn-default" onclick="SortDown();"><i class="fa fa-arrow-down"></i>向下</a><br />
                            <a class="btn btn-default" onclick="DownMost();"><i class="fa fa-arrow-down"></i>最下</a><br />
                        </div>
                    </div>
                </td>
            </tr>
            <tr>
                <td colspan="4" style="padding-top:1px;">
                    <div class="panel-Title" style="border:0px;">
                        监管项目
                        <div class="btn-group" style="float:right;margin-right:10px;">
                            <a id="lr-addRegulatory" class="btn btn-default" onclick="btn_add_Regulatory()"><i class="fa fa-plus"></i>&nbsp;添加监管项</a>
                            @*<script>$('.btn-group').authorizeButton()</script>*@
                        </div>
                    </div>
                    <div style="margin-left:10px;">
                        <table id="gridTable1"></table>
                    </div>
                </td>
            </tr>
        </table>
    </div>
</div>
